package com.neuedu.springmvc.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {


    /**
     * ModelAndView 设置返回数据(要在视图中渲染大数据) 和视图的名字
     * http://localhost:8080/springmvc08/list
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request , HttpServletResponse response){

        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        //request.setAttribute("list",list);
        //request.getRequestDispatcher("/user/list/.jsp").forward(request,response);


        modelAndView.addObject("list",list);
        //modelAndView.setViewName("/WEB-INF/jsp/user/user_list.jsp");
        modelAndView.setViewName("/user/user_list");
        return modelAndView;
        
    }




    @RequestMapping("/list2")
    public ModelAndView list2(HttpServletRequest request , HttpServletResponse response) throws Exception {

        ModelAndView modelAndView = new ModelAndView();

        //模拟从数据库中查询了10个用户
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }


        if(1==1){
            throw new Exception("主动产生 Exception");
        }
        //request.setAttribute("list",list);
        //request.getRequestDispatcher("/user/list/.jsp").forward(request,response);


        modelAndView.addObject("list",list);
        //modelAndView.setViewName("/WEB-INF/jsp/user/user_list.jsp");
        modelAndView.setViewName("/user/user_list");
        return modelAndView;

    }



}
